<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <header style="height: 100%">
            <div class="left">
                <table style="width:100%">
                    <tr>
                        <td>
                            <form class="form-inline" onsubmit="return false">
                                <div class="form-group">
                                     标题:
                                    <input id="subject" type="text" class="form-control" placeholder="标题">
                                    toEmail：
                                    <input id="toEmail" type="text" class="form-control" placeholder="toEmail">
                                    状态：
                                    <select class="form-control input-sm" id="status">
                                        <option value="">全部</option>
                                        <option value="0">草稿</option>
                                        <option value="1">发送失败</option>
                                        <option value="2">发送成功</option>
                                    </select>
                                    <button id="searchBt" class="layui-btn layui-btn-sm" permission="mail:query"><i class="layui-icon">&#xe615;</i>搜索</button>
                                </div>
                            </form>
                        </td>
                        <td align="right">
                            <button class="layui-btn layui-btn-sm" onclick="location.href='addMail.html'" permission="mail:save">
                                <i class="layui-icon">&#xe608;</i> 添加
                            </button>
                        </td>
                    </tr>
                </table>
            </div>
        </header>

        <div>
            <div class="widget-body no-padding">
                <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                    <thead>
                    <tr>
                    </tr>
                    <tr>
                        <th>标题</th>
                        <th>发送人</th>
                        <th>收件人邮箱</th>
                        <th>发送时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script src="../../js/constant.js"></script>
<script type="text/javascript" src="../../js/libs/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript">
    var pers = checkPermission();
    var example;

    layui.use(['layer','util'], function(){
        var layer = layui.layer;
        var util = layui.util;

        function init(){
            example =
                $('#dt-table').DataTable({
                    "searching": false,
                    "processing": false,
                    "serverSide" : true,
                    "ordering" :false,
                    "language": {
                        "url": "../../js/plugin/datatables/Chinese.lang"
                    },
                    "ajax": {
                        "url" : domainName + "/api-b/mails",
                        "type":"get",
                        "data":function(d){
                            d.subject = $('#subject').val();
                            d.toEmail = $('#toEmail').val();
                            d.status = $('#status').val();
                        },
                        "dataSrc": function (json) {
                            json.recordsTotal=json.total;
                            json.recordsFiltered=json.total;
                            return json.data;
                        },
                        "error":function(xhr, textStatus, errorThrown){
                            var msg = xhr.responseText;
                            console.log(msg);
                            if (xhr.status == 400) {
                                layer.msg(JSON.parse(msg).message);
                            } else if (xhr.status == 401) {
                                localStorage.removeItem("token");
                                layer.msg("token过期，请重新登录", {shift: -1, time: 1000}, function(){
                                    location.href = loginPage;
                                });
                            } else if (xhr.status == 403) {
                                layer.msg('未授权');
                            } else if (xhr.status == 500) {
                                var info = JSON.parse(msg);
                                var exception = info.exception;
                                var message = info.message;
                                layer.msg('系统错误：' + (exception ? exception : message));
                            }
                        }
                    },
                    "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",
                    "columns": [
                        { "data": "subject", "defaultContent": ""},
                        { "data": "username", "defaultContent": ""},
                        { "data": "toEmail", "defaultContent": ""},
                        {
                            "data": "sendTime",
                            "defaultContent": "",
                            "render": function (data, type, row) {
                                if(data){
                                    return util.toDateString(data);
                                }
                                return "";
                            }
                        },
                        {
                            "data": "status",
                            "defaultContent": "",
                            "render": function (data, type, row) {
                                if (data == 0) {
                                    return "草稿";
                                } else if (data == 1) {
                                    return "<span style='color:green'>发送成功</span>";
                                } else {
                                    return "<span style='color:red'>发送失败</span>";
                                }
                            }
                        },
                        {
                            "data": "",
                            "defaultContent": "",
                            "render": function (data, type, row) {
                                var id = row['id'];
                                var status = row['status'];
                                var href = "updateMail.html?id=" + id;
                                var edit = buttonEdit(href, "mail:update", pers);
                                if(status == 1){
                                    edit = "";
                                }

                                var btn = $("<button class='layui-btn layui-btn-xs'><i class='layui-icon'>详情</i></button>");
                                btn.attr("onclick", "showDetail(\'" +id+"\')");
                                var showDetail = btn.prop("outerHTML");
                                return edit + showDetail;
                            }
                        },
                    ],
                } );
        }

        init();
    });


    function showDetail(id){
        layer.open({
            title:"详情",
            type: 2,
            area: ['800px', '600px'],
            maxmin: true,
            shadeClose: true,
            content: ['detail.html?id='+id]

        });
    }


    $("#searchBt").click(function(){
        example.ajax.reload();
    });
</body>
</html>